<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="box-card">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="roleManagement">
            <el-button type="primary" size="small" @click="addRole()">新增角色</el-button>
            <div style="margin-top:30px">
              <el-table
                :data="tableData"
                style="width: 100%"
                stripe
                border
              >
                <el-table-column
                  type="index"
                  label="序号"
                  width="120"
                  align="center"
                />
                <el-table-column
                  prop="name"
                  label="角色"
                  width="240"
                  align="center"
                />
                <el-table-column
                  prop="description"
                  label="描述"
                  align="center"
                />
                <el-table-column
                  prop="date"
                  label="操作"
                  align="center"
                >
                  <template slot-scope="scope">
                    <el-button type="success" size="mini">分配权限</el-button>
                    <el-button type="primary" size="mini" @click="editRole(scope.row.id)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="delRole(scope.row.id)">删除</el-button>
                  </template>

                </el-table-column>
              </el-table>
            </div>

            <div style="float:right;margin-top:20px">

              <el-pagination
                small
                layout="prev, pager, next"
                :total="page.total"
                :current-page="page.page"
                :page-size="page.pagesize"
                @current-change="handleCurrentChange"
              />

            </div>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="companyInformation">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="warning"
            />

            <el-form ref="form" label-width="80px" style="margin-top:50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司邮箱">
                <el-input v-model=" formData.mailbox" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model=" formData.companyPhone" disabled style="width:400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model=" formData.remarks" type="textarea" disabled style="width:400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 编辑弹层--对话框 -->
      <el-dialog :title="type==='addRole'?'新增角色':'编辑角色'" :visible.sync="dialogVisible" @close="cancel">
        <el-form ref="formRole" :model="formRole" :rules="rules">
          <el-form-item
            label="角色名称"
            label-width="100px"
            prop="name"
          >
            <el-input v-model="formRole.name" />
          </el-form-item>
          <el-form-item label="角色描述" label-width="100px">
            <el-input v-model="formRole.description" type="textarea" />
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancel()">取 消</el-button>
          <el-button type="primary" @click="sure()">确 定</el-button>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, delRole, editRole, getDetailRole, addRole } from '@/api/setting.js'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      activeName: 'roleManagement',
      page: {
        page: 1, // 页码
        pagesize: 20, // 每页条数
        total: 10
      },
      tableData: [],
      formData: {},
      formRole: {
        name: '',
        description: ''
      },
      type: 'addRole',
      dialogVisible: false,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]

      }
    }
  },
  computed: {
    ...mapGetters(['companyId'])
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },

  methods: {
    // 获取公司角色列表所有信息
    async getRoleList() {
      console.log('传进去的参数', this.page)
      const { total, rows } = await getRoleList(this.page)
      this.tableData = rows
      console.log(this.tableData)
      this.page.total = total
    },
    // 根据id获取公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId)
      // console.log(this.formData)
    },
    // 删除
    async delRole(id) {
      try {
        await this.$confirm('确定删除该角色？') // 只有点击了确定 才能进入到下方
        await delRole(id) // 调用删除接口
        this.$message.success('删除成功')
        this.getRoleList()
      } catch (error) {
        console.log(error)
      }
    },
    //  点击编辑时，表单数据回显
    async editRole(id) {
      this.formRole = await getDetailRole(id)
      this.dialogVisible = true
      this.type = 'editRole'
    },
    // 新增
    addRole() {
      this.type = 'addRole'
      this.dialogVisible = true
    },

    // 确定按钮
    // 新增角色/编辑角色
    async sure() {
      try {
        await this.$refs.formRole.validate() // 只有通过了表单校验才会发请求
        // formRole这个对象有id就是编辑 没有id就是新增
        if (this.formRole.id) {
          await editRole(this.formRole)
          this.$message.success('编辑成功')
        } else { // 新增角色
          await addRole(this.formRole)
          this.$message.success('新增成功')
        }
        this.getRoleList()// 重新拉取数据
        this.dialogVisible = false // 关闭对话框
      } catch (error) {
        console.log(error)
      }
    },
    // 取消
    cancel() {
      this.formRole = {}// 清空数据
      this.$refs.formRole.resetFields() // 移除表单校验结果
      this.dialogVisible = false
    },

    handleCurrentChange(newpage) {
      console.log(`当前页: ${newpage}`)
      this.page.page = newpage
      this.getRoleList()
    }

  }
}
</script>

<style>

</style>
